package com.wang.controller;


import com.sun.istack.internal.NotNull;
import com.wang.pojo.User;
import com.wang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * @author admin
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    static  boolean flag = false;

    @RequestMapping("toLogin")
    public String Login() {
        return "index";
    }
    @RequestMapping("/doLogin")
    public String toLogin(HttpSession session, HttpServletRequest request) {
//        String username = request.getParameter("username");
//        String pwd = request.getParameter("password");
//        User userInfo = userService.getUserInfo(username, pwd);
//        if (userInfo != null) {
//            session.setAttribute("msg", userInfo);
////            return "dashboard";
//        }
        return "dashboard";
    }

    @RequestMapping("/loginout")
    public String loginOut(HttpSession session) {
//        session.removeAttribute("msg");
        return "index";
    }

    /**
     * 跳转到注册界面
     * @return
     */
    @RequestMapping("/toRe")
    public String toRegister(Model model) {
        if (flag) {
            model.addAttribute("errorInfo", "账号已存在");
        }
        return "register";
    }

    /**
     * 注册逻辑
     * @param user
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/re")
    public  String registerUser(User user, @NotNull HttpServletRequest request,@NotNull Model model) {
        String name = request.getParameter("name");
        User one = userService.getUserOne(name);
        String str = "";
        if (one == null) {
            userService.insertUser(user);
            flag = false;
            str = "redirect:/login";
        } else {
            flag = true;
            str = "redirect:/toRe";
        }
        return str;
    }

}
